/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomGamePane.java
 *
 * Created on Jan 8, 2012, 10:34:23 AM
 */
package views.panes;

import controller.human.CustomRole;
import controller.human.Role;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import views.MafiaFrame;

/**
 *
 * @author darkdev
 */
public class CustomGamePane extends javax.swing.JPanel {

    MafiaFrame gui;
    JPanel cancel;
    ArrayList<CustomRole> roles = new ArrayList<CustomRole>();

    /** Creates new form CustomGamePane */
    public CustomGamePane(MafiaFrame gui, JPanel cancel) {
        initComponents();
        rolesPanel.setMaximumSize(new Dimension(WIDTH, HEIGHT -120));
        this.gui = gui;
        this.cancel = cancel;
       

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        Add = new javax.swing.JButton();
        Save = new javax.swing.JButton();
        Cancel = new javax.swing.JButton();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 120), new java.awt.Dimension(0, 120), new java.awt.Dimension(32767, 120));
        rolesPanel = new javax.swing.JPanel();

        jLabel1.setText("Custom Roles Selection");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        Add.setText("Add Role");
        Add.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                AddMouseClicked(evt);
            }
        });

        Save.setText("Save");
        Save.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SaveMouseClicked(evt);
            }
        });

        Cancel.setText("Cancel");
        Cancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CancelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout rolesPanelLayout = new javax.swing.GroupLayout(rolesPanel);
        rolesPanel.setLayout(rolesPanelLayout);
        rolesPanelLayout.setHorizontalGroup(
            rolesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 466, Short.MAX_VALUE)
        );
        rolesPanelLayout.setVerticalGroup(
            rolesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 84, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(Add)
                        .addGap(18, 18, 18)
                        .addComponent(Save)
                        .addGap(24, 24, 24)
                        .addComponent(Cancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(125, Short.MAX_VALUE))
            .addComponent(rolesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {Add, Cancel, Save});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rolesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(Save)
                        .addComponent(Cancel)
                        .addComponent(Add)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {Cancel, Save});

    }// </editor-fold>//GEN-END:initComponents

    private void SaveMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SaveMouseClicked
        // TODO add your handling code here:
        //save all roles and the number of players and go to enlisment screen
       int nrRoles = rolesPanel.getComponentCount();
       int nrPlayers = 0;
       for (int i =0;i<nrRoles;i++)
       {
           CustomRolePane cp = (CustomRolePane) rolesPanel.getComponent(i);
           String RoleName = cp.getRoleName();
           int nr =cp.getNoPlayers();
           nrPlayers+=nr;
           if(RoleName.isEmpty()||nr<=0)
               break;
           CustomRole cr = new CustomRole(Role.valueOf(RoleName), nr);
           roles.add(cr);
       }
       gui.settings.numberPlayers = nrPlayers;
       gui.setCustomRoles(roles);
       gui.update(new PlayersRegistrationPane(gui,this));
    }//GEN-LAST:event_SaveMouseClicked

    private void CancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CancelMouseClicked
        // TODO add your handling code here:
        gui.update(cancel);
    }//GEN-LAST:event_CancelMouseClicked

    public void deleteRole(int x, int y)
    {
        Component comp =rolesPanel.getComponentAt(x, y);
          this.rolesPanel.remove(comp);
          revalidate();
        
    }
    private void AddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AddMouseClicked
        // TODO add your handling code here:
        CustomRolePane cpanel = new CustomRolePane(this);
        this.rolesPanel.setLayout(new BoxLayout(this.rolesPanel, BoxLayout.PAGE_AXIS));
        this.rolesPanel.add(cpanel);
        revalidate();

    }//GEN-LAST:event_AddMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Add;
    private javax.swing.JButton Cancel;
    private javax.swing.JButton Save;
    private javax.swing.Box.Filler filler1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel rolesPanel;
    // End of variables declaration//GEN-END:variables
}
